using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using MinesweeperPlus.Scripts.Configs;
using MinesweeperPlus.Scripts.Utils;

namespace MinesweeperPlus.Scripts.UI
{
    public class UIElement : VisualElement
    {   
        #region Variables
        private IconsConfig _iconsConfig;
        private ColorSchemeConfig _colorSchemeConfig;
        private Dictionary<VisualElement, VectorImage> _dictionary;

        protected VisualElement Panel;
        #endregion
        #region Properties
        public StyleSheet StyleSheet { get; private set; }
        public IconsConfig IconsConfig
        {
            get => _iconsConfig;
            set
            {
                if (_iconsConfig == value)
                    return;

                _iconsConfig = value;
                SetIconsConfig();
            }
        }
        public ColorSchemeConfig ColorSchemeConfig
        {
            get => _colorSchemeConfig;
            set
            {
                if (_colorSchemeConfig == value)
                    return;

                _colorSchemeConfig = value;
                UpdateColorScheme();
            }
        }
        #endregion

        #region UIElement Methods
        public UIElement() : this(
            Resources.Load<IconsConfig>(Constants.CFG_ICONS_NAME),
            Resources.Load<StyleSheet>(Constants.UI_STYLE_SHEET_NAME)) { }
        public UIElement(IconsConfig iconsConfig, StyleSheet styleSheet)
        {
            _iconsConfig = iconsConfig;
            StyleSheet = styleSheet;
            _dictionary = new();

            this.styleSheets.Add(StyleSheet);
            this.AddToClassList(Constants.UI_CONTAINER_STYLE);
            CreatePanel();

            SetIconsConfig();
        }
        #endregion

        #region Other Methods
        protected virtual void UpdateColorScheme() { }

        protected virtual void CreatePanel() 
        { 
            Panel = new VisualElement();
            this.hierarchy.Add(Panel);
            Panel.AddToClassList(Constants.UI_PANEL_STYLE);
        }

        protected virtual IMGUIContainer CreateIMGUIContainer(
            VectorImage vectorImage, 
            params string[] classesNames)
        {
            var imguiContainer = new IMGUIContainer();
            foreach (var className in classesNames)
                imguiContainer.AddToClassList(className);
            _dictionary.Add(imguiContainer, vectorImage);
            return imguiContainer;
        }

        protected virtual Button CreateButton(Action buttonClicked, params string[] classesNames)
        {
            var button = new Button();
            foreach (var className in classesNames)
                button.AddToClassList(className);
            Panel.Add(button);
            button.clicked += buttonClicked;
            return button;
        }
        protected virtual Button CreateButton(
            IMGUIContainer imguiContainer, 
            Action buttonClicked,
            params string[] classesNames)
        {
            var button = CreateButton(buttonClicked);
            foreach (var className in classesNames)
                button.AddToClassList(className);
            button.Add(imguiContainer);
            return button;
        }

        private void SetIconsConfig()
        {
            foreach (var pair in _dictionary)
                pair.Key.SetBackgroundImage(pair.Value);
        }
        #endregion
    }
}
